<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>图片转换Pdf</title>
    <style>
        .submitButton {
            margin-top: 20px;
            margin-left: 150px;
            background-color: #e37e10;
            border-radius: 10px;
            border: 1px solid #ff8300;
        }
    </style>
</head>
<body>
<div style="text-align: center">
    <h1>图片转换pdf工具</h1>
    <!-- 这里项目路径写死了，优化 -->
    <!--<form id="imgToPdf" action="/spring_html/image/to/pdf" enctype="multipart/form-data" method="post" onsubmit="return allowFileType()">-->
    <form id="imgToPdf" enctype="multipart/form-data" method="post" onsubmit="return allowFileType()">
        <input type="file" id="file" name="file" placeholder="请选择图片" onchange="allowFileType()"
               style="border: 1px solid black;"><br>
        <input type="submit" value="一键转换pdf文件" class="submitButton" onclick="updateAction();">
    </form>
</div>
</body>
<script>

    //获取项目根路径
    function getRootPath() {
        //获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp
        let curWwwPath = window.document.location.href;
        //获取主机地址之后的目录，如： uimcardprj/share/meun.jsp
        let pathName = window.document.location.pathname;
        let pos = curWwwPath.indexOf(pathName);
        //获取主机地址，如： http://localhost:8083
        let localhostPaht = curWwwPath.substring(0, pos);

        //获取带"/"的项目名，如：/uimcardprj
        return pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
        /*let projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
        return (localhostPaht + projectName);*/
    }

    function updateAction() {
        document.getElementById('imgToPdf').action = getRootPath() + '/image/to/pdf';
    }

    function allowFileType() {
        // document.getElementById('imgToPdf').action = getRootPath() + '/image/to/pdf';
        let file = document.getElementById("file").files[0];
        let fileName = file.name;
        console.log(fileName)
        let fileSize = file.size;
        console.log(fileSize)
        let suffix = fileName.substring(fileName.lastIndexOf("."), fileName.length).toLowerCase();
        if ('.jpg' != suffix && '.png' != suffix) {
            alert("目前只允许传入.jpg或者.png格式的图片！");
            return false;
        }
        if (fileSize > 2 * 1024 * 1024) {
            alert("上传图片不允许超过2MB!");
            return false;
        }
        return true;
    }

</script>
</html>
